Shader "Study/Chapter6/NormalTexture"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _BumpMap("BumpMap",2D) = "bump" {}
        _BumpScale("BumpScale",Range(-10,10)) = 1.0
        [Toggle(USENORMAL)]_UseNormal("UseNormal",int) = 1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        Pass
        {
            CGPROGRAM
            // Upgrade NOTE: excluded shader from DX11; has structs without semantics (struct appdata members normal)
            //#pragma exclude_renderers d3d11
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile _ USENORMAL

            #include "Lighting.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                float4 tangent : TANGENT;
                float3 normal : NORMAL;
            };

            struct v2f
            {
                float4 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                float3 lightDir : TEXCOORD1;
                float3 viewDir : TEXCOORD2;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            sampler2D _BumpMap;
            float4 _BumpMap_ST;
            float _BumpScale;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv.xy = v.uv.xy * _MainTex_ST.xy + _MainTex_ST.zw;
                o.uv.zw = v.uv.xy * _BumpMap_ST.xy + _BumpMap_ST.zw;

                //  (切线空间计算)
                float3 worldNormal = UnityObjectToWorldNormal(v.normal);
                float3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
                // 副切线
                fixed3 worldBinormal = cross(worldNormal,worldTangent) * v.tangent;
                // 转切线空间矩阵
                float3x3 worldToTangent = float3x3(worldTangent,worldBinormal,worldNormal);

                o.lightDir = mul(worldToTangent,WorldSpaceLightDir(v.vertex));
                o.viewDir = mul(worldToTangent,WorldSpaceViewDir(v.vertex));
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 albedo = tex2D(_MainTex,i.uv.xy);
                #if USENORMAL
                    fixed3 tangentLightDir = normalize(i.lightDir);
                    fixed3 tangentViewDir = normalize(i.viewDir);

                    fixed4 normalTex = tex2D(_BumpMap,i.uv.zw);

                    fixed3 tangentNormal = UnpackNormal(normalTex);
                    tangentNormal.xy *= _BumpScale;
                    tangentNormal.z = sqrt(1.0 - saturate(dot(tangentNormal.xy,tangentNormal.xy)));
                    
                    // 这里只算漫反射 计算的是切线空间下的漫反射
                    fixed3 diffuse = max(0,dot(tangentNormal,tangentLightDir)) * 0.5 + 0.5;
                    return fixed4(albedo * diffuse,1.0);
                #else
                    return albedo;
                #endif
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
}
